package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsListActivity;
import com.zadtek.shopmall.bean.WithDrawListBean;
import com.zadtek.shopmall.bean.WithdrawItem;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Zackv on 2018/4/12.
 */

public class WithDrawRecordActivity extends AbsListActivity {
    private List<WithdrawItem> data = new LinkedList<>();
    private AccountAdapter accountAdapter;
    private int page = 1;
    private int pagesize =10;
    @Override
    protected String getTopTitle() {
        return "提现记录";
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        accountAdapter = new AccountAdapter(R.layout.withdraw_item,data);
        return accountAdapter;
    }

    @Override
    public void loadMore() {
        requestData();
    }

    @Override
    protected void requestData() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("uid",69);
        param.put("pagesize",pagesize);
        NetTool.getApi().getWithDrawList(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<WithDrawListBean>>() {
                    @Override
                    public void onData(BaseResponse<WithDrawListBean> value) {
                        if(value.getCode() == 0 && value.getData()!=null && value.getData().getLog_list()!=null){
                            if(value.getData().getLog_list().size() != 0){
                                data.addAll(value.getData().getLog_list());
                                accountAdapter.notifyDataSetChanged();
                                accountAdapter.loadMoreComplete();
                                page++;
                            }else{
                                accountAdapter.loadMoreEnd();
                            }
                        }else{
                            accountAdapter.loadMoreEnd();
                        }
                    }
                });
    }

    @Override
    public void OnItemClick(int position) {
        startActivity(new Intent(this,WithdrawItemDetailActivity.class).putExtra("data",data.get(position)));
    }

    private class  AccountAdapter extends BaseQuickAdapter<WithdrawItem,BaseViewHolder>{

        public AccountAdapter(int layoutResId, @Nullable List<WithdrawItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, WithdrawItem item) {
            helper.setText(R.id.tv_title,item.getType());
            helper.setText(R.id.tv_money,item.getAmount());
            helper.setText(R.id.tv_time,item.getAdd_time());
            helper.setText(R.id.tv_status,item.getPay_status());
        }
    }
}
